/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include <windows.h>
#include <math.h>

#define WIDTH 800
#define HEIGHT 600
#define PI 3.14159265358979323846

// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        // 设置绘图模式为抗锯齿
        SetBkMode(hdc, TRANSPARENT);
        HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
        HBRUSH hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
        SelectObject(hdc, hPen);
        SelectObject(hdc, hBrush);

        // 绘制坐标轴
        MoveToEx(hdc, 50, HEIGHT / 2, NULL);
        LineTo(hdc, WIDTH - 50, HEIGHT / 2);
        MoveToEx(hdc, WIDTH / 2, 50, NULL);
        LineTo(hdc, WIDTH / 2, HEIGHT - 50);

        // 绘制正弦曲线
        for (int x = 50; x < WIDTH - 50; x++) {
            double t = (double)(x - 50) / (WIDTH - 100) * 2 * PI;
            double yValue = sin(t);
            int y = (int)(HEIGHT / 2 - yValue * (HEIGHT / 4));
            if (x == 50) {
                MoveToEx(hdc, x, y, NULL);
            }
            else {
                LineTo(hdc, x, y);
            }
        }

        DeleteObject(hPen);
        DeleteObject(hBrush);
        EndPaint(hwnd, &ps);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    // 注册窗口类
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "SineWaveWindow";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    RegisterClass(&wc);

    // 创建窗口
    HWND hwnd = CreateWindow(wc.lpszClassName, "Sine Wave", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);

    // 显示窗口
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
